package com.hoopchina.ui.search;

// import class


import com.hoopchina.news.R;
import android.content.Context;
import android.text.Editable;
import android.text.TextWatcher;
import android.util.AttributeSet;
import android.view.KeyEvent;
import android.view.LayoutInflater;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.view.View;
import android.view.inputmethod.InputMethodManager;
import android.widget.Button;

/**
 * SearchBar
 * 搜索栏自定义控件
 * 
 * @author Guo Baiyu
 */
public class SearchBar extends LinearLayout {
    private Context                  mContext                  = null;
    private ISearchBarChangeObserver mChangeObserver           = null;
    private Button                   mButtonSearch             = null;
    private EditText                 mEditText                 = null;
    public static final int          SEARCHBAR_MAX_LEN         = 50;
    public static final int          SEARCH_BAR_TEXT_CHANGE    = 0;
    public static final int          SEARCH_BAR_EDITER_CLICKED = 1;
    public static final int          SEARCH_BAR_EDITER_ENTER   = 2;
    public static final int          SEARCH_BAR_BUTTON_CLICKED = 3;
    public String                    mDispSearchText           = "";
    private String                   mCurrentSearchText        = "";
    
    void setCurrentSearchText(String strSearch)
    {
        mCurrentSearchText = strSearch;
        mEditText.setText(strSearch);
    }
    
//    void setDisplayText( String dispText )
//    {
//        mDispSearchText = dispText;
//    }
//    
//    mEditText.setText( mDispSearchText );

    
    public void setTextCurrentSearch() {
        setSearchEditText( mCurrentSearchText );
        int length = mCurrentSearchText.length();
        if ( length > SearchBar.SEARCHBAR_MAX_LEN ) {
            length = SearchBar.SEARCHBAR_MAX_LEN;
        }
    }

    /**
     * (non-Javadoc)
     * 
     * @see android.view.ViewGroup#dispatchKeyEvent(android.view.KeyEvent)
     */
    @Override
    public boolean dispatchKeyEvent( KeyEvent event ) {
        if ( event.getKeyCode() == KeyEvent.KEYCODE_ENTER ) {
            mCurrentSearchText = mEditText.getText().toString().trim();
            mChangeObserver.onSearchBarChange( SEARCH_BAR_EDITER_ENTER, mCurrentSearchText ); 

        }
        return super.dispatchKeyEvent( event );
    }

    /**
     * 
     * @return the mEditText
     */
    public EditText getEditText() {
        return mEditText;
    }

    /**
     * 
     * @param context
     * @param aSet
     */
    public SearchBar( Context context, AttributeSet aSet ) {
        super( context, aSet );

        mContext = context;
        init();
    }

    /**
     * 
     * @param context
     */
    public SearchBar( Context context ) {
        super( context );

        mContext = context;
        init();
    }

    /**
     * 共通初始化
     */
    private void init() {
        // 布局
        String infServiString = Context.LAYOUT_INFLATER_SERVICE;
        LayoutInflater lInflater;
        lInflater = ( LayoutInflater ) getContext().getSystemService( infServiString );
        lInflater.inflate( R.layout.searchbar, this, true );
        
        initEditText();
        initSearchButton();
    }

    /**   
     * 输入框初始化
     */
    private void initEditText() {
        // mEditTextF = ( EditText ) findViewById( R.id.editTextF );
        // mEditTextF.setFocusable(true);

        mEditText = ( EditText ) findViewById( R.id.editTextSearchBar );

//        dispSoftMethodInput( true );

        // set输入框点击事件监听
        mEditText.setOnClickListener( new OnClickListener() {
            @Override
            public void onClick( View v ) {
                dispSoftMethodInput(true);
                setEditFocus();
                mDispSearchText = mEditText.getText().toString();
                mChangeObserver.onSearchBarChange( SEARCH_BAR_EDITER_CLICKED, mDispSearchText ); // go to get json

                return;
            }
        } );

        mEditText.addTextChangedListener( new TextWatcher() {
            public void afterTextChanged( Editable s ) {
                mDispSearchText = s.toString().trim();
                mChangeObserver.onSearchBarChange( SEARCH_BAR_TEXT_CHANGE, mDispSearchText );
            }

            @Override
            public void beforeTextChanged( CharSequence s, int arg1, int arg2, int arg3 ) {}

            @Override
            public void onTextChanged( CharSequence s, int arg1, int arg2, int arg3 ) {}
        } );

    }
   
    /**
	 * 
	 */
    public void setFocus() {
        
    }

    /**
     * 搜索按钮初始化
     */
    private void initSearchButton() {
        mButtonSearch = ( Button ) findViewById( R.id.buttonSearchBar );
        mButtonSearch.getPaint().setFakeBoldText( true );

        /*
         * set 搜索按钮点击事件
         */
        mButtonSearch.setOnClickListener( new OnClickListener() {
            @Override
            public void onClick( View v ) {
                mCurrentSearchText = mEditText.getText().toString().trim();
                mChangeObserver.onSearchBarChange( SEARCH_BAR_BUTTON_CLICKED, mCurrentSearchText );
            }
        } );
    }

    /**
     * 设置输入框的内容
     * 
     * @param text：设置searchbar输入框的内容
     */
    public void setSearchEditText( String text ) {
    	if(null == text)
    		return;
        int length = text.length();
        if ( length > SearchBar.SEARCHBAR_MAX_LEN ) {
            length = SearchBar.SEARCHBAR_MAX_LEN;
            text = text.substring( 0, length );
        }
       
        mEditText.setText( text );
        mEditText.setSelection( length );
    }

    /**
     * 获得输入框的内容
     * 
     * @return searchbar输入框的内容
     */
    public String getSearchEditText() {
        return mEditText.getText().toString();
    }

    /**
     * 
     * @param isEnabled
     */
    public void setSearchBarEnabled( boolean isEnabled ) {
        mButtonSearch.setEnabled( isEnabled );
        mEditText.setEnabled( isEnabled );
    }

    void dispSoftMethodInput( boolean isDisp ) {
        InputMethodManager imm = ( InputMethodManager ) mContext
                .getSystemService( Context.INPUT_METHOD_SERVICE );
        
        if ( isDisp ) {
            imm.toggleSoftInput( 0, InputMethodManager.HIDE_IMPLICIT_ONLY );
            mEditText.requestFocus();
        }
        else {
        	mButtonSearch.requestFocus();
            imm.hideSoftInputFromWindow( mEditText.getWindowToken(), 0 );
        }
    }

    void setObserver( ISearchBarChangeObserver observer ) {
        mChangeObserver = observer;
    }
//   public String delteSingleQuotes(String str)
//    { 
//    }
    
    public void setEditFocus() { 
        if(!mEditText.isFocused()){
            mEditText.requestFocus();
        }
    }
    
    public void clearEditFocus() { 
        if(mEditText.isFocused()){
        	  mEditText.clearFocus();
        	  mButtonSearch.requestFocus();
        }
    }
}
